import { AchiveId, AchiveItemId } from '../define/GameDefine';
import { Item } from './Item';

const SuccessedMap = {
  [AchiveId.Health]: {
    [AchiveItemId.Apple]: 1,
    [AchiveItemId.Medic]: 1,
    [AchiveItemId.YaLing]: 1,
    [AchiveItemId.YanZhao]: 1,
    [AchiveItemId.Heart]: 1,
  },
  [AchiveId.Work]: {
    [AchiveItemId.Clock]: 1,
    [AchiveItemId.Offer]: 1,
    [AchiveItemId.MacBook]: 1,
    [AchiveItemId.Card]: 1,
    [AchiveItemId.Heart]: 1,
  },
};

const MIN_SUCCESS_COUNT = 3;

export class ItemManager {
  public static CheckSuccessed(items: Item[]): { winType: number; winIds?: number[] } {
    const ids = items
      .filter((item) => item !== undefined)
      .map((item) => item.id)
      .filter((id) => id > 100);
    if (ids.length < MIN_SUCCESS_COUNT) {
      return { winType: -1 };
    }

    const winKeys = Object.keys(SuccessedMap);
    let winIds = [];

    for (let j = 0; j < winKeys.length; j++) {
      const winMap = { ...SuccessedMap[winKeys[j]] };
      let achiveCount = 0;
      winIds = [];
      for (let i = 0, len = ids.length; i < len; i++) {
        const left = len - i;
        if (left < MIN_SUCCESS_COUNT - achiveCount) {
          break;
        }
        if (winMap[ids[i]]) {
          delete winMap[ids[i]];
          winIds.push(ids[i]);
          achiveCount++;
        }
      }
      if (achiveCount >= MIN_SUCCESS_COUNT) {
        return { winType: parseInt(winKeys[j], 10), winIds };
      }
    }
    return { winType: -1 };
  }
}
